import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/event/index.dart';
import 'package:flutter_demo/core/common/event/login_event.dart';

/*
*StatelessWidget 只有createElement()和build()两个生命周期函数*/
/*StatefulWidget生命周期分三种状态
* 1.初始化时期
*   createState();initState();
* 2.更新时期
*   didChangeDependencies();build();didUpdateWidget()
* 3.销毁时期
*   deactivate();dispose();
*/
class EventView extends StatefulWidget {
  static const String routeName = "/event/eventView";
  const EventView({super.key});
  @override
  State<EventView> createState() => _EventView();
}

class _EventView extends State<EventView> {
  late StreamSubscription stream;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("event test"),
          centerTitle: true,
        ),
        body: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          Container(
            height: 200,
            width: 200,
            color: Colors.red,
            child: Align(
              alignment: Alignment.center,
              child: ElevatedButton(
                  child: const Text("event fire"),
                  onPressed: () {
                    print("发送事件");
                    eventBus.fire(LoginEvent(true));
                  }),
            ),
          )
        ]));
  }

  void _requestData() {
    print("收到事件");
  }

  @override
  void initState() {
    super.initState();
    stream = eventBus.on<LoginEvent>().listen((event) {
      _requestData();
    });
    print("初始化....initSate");
  }

  @override
  void dispose() {
    super.dispose();
    stream.cancel();
    print("这里是deactivate()函数");
  }
}
